/*
* @Author: dengjiayao
* @Date:   2017-04-10 14:57:41
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-04-28 14:48:01
*/

import { modal } from 'vue-strap'
import extend from 'extend'
import { showMsg, addAIGuide, updateAIGuide } from '../../vuex/action'

export default {
  name: 'ai-guide-config',
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xAdd: addAIGuide,
      xUpdate: updateAIGuide
    }
  },
  data() {
    return {
      showModal: false,
      actionType: 'CREATE', // CREATE, UPDATE
      form: {}
    }
  },
  computed: {
    title() {
      return +this.form.type === 1 ? '配置欢迎语' : '配置无结果引导语'
    }
  },
  methods: {
    check() {
      var fields = ['word']
      for (let i = 0, len = fields.length; i < len; i++) {
        let val = this.form[fields[i]] + ''
        if (!val.length) {
          return Promise.reject({
            error_msg: '* 标记的为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    submit() {
      this.check()
        .then(() => {
          let params = extend({}, this.form)
          if (this.actionType === 'CREATE') {
            return this.xAdd(params)
          } else {
            return this.xUpdate(params)
          }
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
